const express = require('express')
const router = express.Router()
const multer = require('multer')

const db = require('../sql/db')
const upload = multer({ dest: 'uploads/' })



// 发布新文章
router.post('/my/article/add', upload.single('cover_img'), function (req, res) {
    const { title, state, content, cate_id, pub_date } = req.body
    const { filename } = req.file
    db(`INSERT INTO article SET title = '${title}', cover_img = '${filename}',pub_date='${pub_date}',content='${content}',state='${state ? state : '草稿'}',cate_id=${cate_id},is_delete='0',author_id='3'`, function (err, data) {
        console.log(err);
        console.log(data);
        if (err || data.affectedRows == 0) return res.send({
            status: 1,
            message: '发布文章失败！'
        })
        res.send({
            status: 0,
            message: '发布文章成功！'
        })
    })
})


// 获取文章的列表数据
router.get('/my/article/list', function (req, res) {
    let { pagenum, pagesize, state, cate_id } = req.query
    console.log(req.query);
    if (!cate_id) cate_id = '.';
    if (!state) state = '.';
    if (!pagenum) pagenum = 1;
    if (!pagesize) pagesize = 6;
    const pages = pagenum * pagesize - pagesize
    db(`select * from article where is_delete=0 and state REGEXP '${state}' and cate_id REGEXP '${cate_id}'  order by id desc limit ${pages},${pagesize}`, function (err, data) {
        if (err || data.length == 0) return res.send({
            status: 1,
            message: '获取文章失败！'
        })
        res.send({
            "status": 0,
            "message": "获取文章列表成功！",
            data,
            count: data.length,
            pagenum: Math.ceil(data.length / pagesize)
        })
    })
})


// 根据 Id 删除文章数据
router.get('/my/article/delete/:id', function (req, res) {
    let { id } = req.params
    console.log(req.params);
    db(`delete  from article where id=${id}`, function (err, data) {
        console.log(data);
        if (err || data.affectedRows == 0) return res.send({
            status: 1,
            message: '删除文章失败！'
        })
        res.send({
            "status": 0,
            "message": "删除文章成功",
        })
    })
})


// 根据 Id 获取文章详情
router.get('/my/article/:id', function (req, res) {
    let { id } = req.params
    console.log(req.params);
    db(`select * from article where id=${id} and is_delete=0`, function (err, data) {

        if (err || data.length == 0) return res.send({
            status: 1,
            message: '获取文章失败！'
        })
        res.send({
            "status": 0,
            "message": "获取文章成功",
            data
        })
    })
})


// 根据 Id 更新文章信息
router.post('/my/article/edit', upload.single('cover_img'), function (req, res) {
    const { id, title, cate_id, content, state } = req.body
    const { filename } = req.file
    db(`update  article  set title='${title}',cate_id='${cate_id}',content='${content}',cover_img='${filename}',state='${state}' where is_delete=0 and id=${id}`, function (err, data) {
        if (err) return res.send({
            status: 1,
            message: '修改文章失败！'
        })
        res.send({
            status: 0,
            message: '修改文章成功！'
        })
    })
})

module.exports = router